package com.dd.cloud.open.controller.api;

import com.dd.cloud.open.enums.ResponseEnum;
import com.dd.cloud.open.exception.ApiException;
import com.dd.cloud.open.req.api.*;
import com.dd.cloud.open.res.api.*;
import com.dd.cloud.open.service.IApiService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zk
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    private IApiService apiService;

    @Resource
    public void setApiService(IApiService apiService) {
        this.apiService = apiService;
    }

    /**
     * ai智能诊断接口
     *
     * @param request
     * @param response
     * @param req
     * @param bs
     * @return
     */
    @PostMapping("/ask")
    public ResponseEntity<Response<DiagnoseRes>> ask(HttpServletRequest request, HttpServletResponse response, @Valid @RequestBody DiagnoseReq req, BindingResult bs) {
        if (bs.hasErrors()) {
            throw new ApiException(ResponseEnum.LACK_PARAMETER);
        }
        DiagnoseRes res = apiService.ask(request, response, req);
        return ResponseEntity.ok(new Response<>(ResponseEnum.SUCCESS.getCode(), res));
    }

    /**
     * 查询科室接口
     *
     * @param request
     * @param response
     * @param req
     * @param bs
     * @return
     */
    @GetMapping("/department")
    public ResponseEntity<Response<List<DepartmentRes>>> department(HttpServletRequest request, HttpServletResponse response, @Valid AuthReq req, BindingResult bs) {
        if (bs.hasErrors()) {
            throw new ApiException(ResponseEnum.LACK_PARAMETER);
        }
        List<DepartmentRes> res = apiService.department(request, response, req);
        return ResponseEntity.ok(new Response<>(ResponseEnum.SUCCESS.getCode(), res));
    }


    /**
     * 查询科室疾病接口以及子疾病接口
     *
     * @param request
     * @param response
     * @param req
     * @param bs
     * @return
     */
    @GetMapping("/disease")
    public ResponseEntity<Response<List<DiseaseRes>>> disease(HttpServletRequest request, HttpServletResponse response, @Valid DiseaseReq req, BindingResult bs) {
        if (bs.hasErrors()) {
            throw new ApiException(ResponseEnum.LACK_PARAMETER);
        }
        List<DiseaseRes> res = apiService.disease(request, response, req);
        return ResponseEntity.ok(new Response<>(ResponseEnum.SUCCESS.getCode(), res));
    }

    /**
     * 查询疾病症状接口
     *
     * @param request
     * @param response
     * @param req
     * @param bs
     * @return
     */
    @GetMapping("/symptom")
    public ResponseEntity<Response<List<SymptomRes>>> symptom(HttpServletRequest request, HttpServletResponse response, @Valid SymptomReq req, BindingResult bs) {
        if (bs.hasErrors()) {
            throw new ApiException(ResponseEnum.LACK_PARAMETER);
        }
        List<SymptomRes> res = apiService.symptom(request, response, req);
        return ResponseEntity.ok(new Response<>(ResponseEnum.SUCCESS.getCode(), res));
    }

    /**
     * 查询疾病专家医生信息接口
     *
     * @param request
     * @param response
     * @param req
     * @param bs
     * @return
     */
    @GetMapping("/doctor")
    public ResponseEntity<Response<DoctorRes>> doctor(HttpServletRequest request, HttpServletResponse response, @Valid DoctorReq req, BindingResult bs) {
        if (bs.hasErrors()) {
            throw new ApiException(ResponseEnum.LACK_PARAMETER);
        }
        DoctorRes res = apiService.doctor(request, response, req);
        return ResponseEntity.ok(new Response<>(ResponseEnum.SUCCESS.getCode(), res));
    }
}